<?php

$html = '';
if (count($data['errors']) == 0) {
    $html .= '<div class="bookProfile"><h2><a href="index.php?p=book&book_id=' . $data['book']['book_id'] . '">' . htmlspecialchars($data['book']['book_title']) . '</a></h2>';
    $html .= '<div class="bookAuthors">';

    $all_authors = array();
    foreach ($data['book']['authors'] as $author) {
        $all_authors[] = '<a href="index.php?p=author&author_id=' . $author['id'] . '">' . htmlspecialchars($author['name']) . '</a>';
    }
    $html .= implode(' | ', $all_authors) . '</div></div>';

    $comments_count = count($data['book']['comments']);
    if ($comments_count > 0) {
        $html .= '<div id="commentsContainer">';
        $html .= '<p class="commentsCount"><span class="commentNumber">' . $comments_count . '</span> коментара</p>';

        foreach ($data['book']['comments'] as $comment) {
            $html .= '<div class="userInfo"><span class="commentNumber">' . $comments_count
                    . '</span>. <a href="index.php?p=user&user_id='
                    . $comment['user_id'] . '">' . htmlspecialchars($comment['username']) . '</a> | '
                    . ' ' . $comment['creation_date'] . '</div>';
            $html .= '<div class="userText">' . htmlspecialchars($comment['content']) . '</div>';
            $comments_count--;
        }
        $html .= '</div>';
    } else {
        $html .= '<p class="commentsCount"><span class="commentNumber">0</span> коментара</p>';
    }

    if (isset($_SESSION['isLogged']) && $_SESSION['isLogged'] == true) {
        $html .= '<div id="newComment">
                    <p id="newCommentCaption">Коментирай</p>
                    <form method="POST" action="index.php?p=book&book_id=' . $data['book']['book_id'] . '">
                        <textarea required="" cols="60" rows="6" name="bookComment"></textarea>
                        <br/>
                        <input type="submit" value="Добави коментар">
                     </form>
                 </div>';
    } else {
        $html .= '<p class="bottomLogin">За да коментирате трябва първо да се впишете <a href="index.php?p=login">Вход</p>';
    }
} else {
    foreach ($data['errors'] as $error) {
        $html .= '<p class="error">' . $error . '</p>';
    }
}

echo $html;